# plugs/clone.py
#
#

""" clone the bot so we don't have to reboot on every change """

__copyright__ = 'this file is in the public domain'

from ommbot.world import add_cmnd, add_example
import ommbot.world
import ommbot.generic
import glob
import os
import sys

def handle_clone(bot, ievent):
    ommbot.world.plugs.exit()
    for i in glob.glob('ommbot' + os.sep + '*.py'):
        if '__' in i or 'world' in i:
            continue
        try:
            reload(sys.modules[i[:-3].replace(os.sep,'.')])
        except:
            ommbot.generic.handle_exception()
            world.log and world.log.log(10, 'clone', "can't reload %s" % i)
    ommbot.world.plugs.restart()
    ommbot.world.fleet.clone(ievent)
    ievent.reply('done')

add_cmnd('clone', handle_clone, 'OPER')
add_example('clone', 'clone the bot .. reload without reboot', 'clone')
